package com.app.micaihu.adapter.PagerAdapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.app.micaihu.app.AppApplication;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.custom.view.PairViewPager;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.baidu.mobstat.StatService;

import java.util.ArrayList;
import java.util.List;

/**
 * 新闻轮播图适配器
 *
 * @author linkai
 */
public class NewsPicPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener, OnClickListener {
    private ArrayList<ImageView> list;
    private Context context;
    private TextView titleView;
    private ViewGroup smallList;
    private List<NewsEntity> pics;
    private PairViewPager viewPager;
    private int pagePosition;

    private LinearLayout.LayoutParams smallParams;
    private LinearLayout.LayoutParams bigParams;

    public NewsPicPagerAdapter(ArrayList<ImageView> list, ViewGroup smalllist, List<NewsEntity> pics, TextView titile, Context context, PairViewPager viewPager) {
        this.list = list;
        this.viewPager = viewPager;
        this.context = context;
        this.smallList = smalllist;
        this.titleView = titile;
        this.pics = pics;
    }

    @Override
    public int getCount() {
        return list != null && !list.isEmpty() ? list.size() : 0;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        container.addView(list.get(position));
        list.get(position).setOnClickListener(this);
        return list.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        container.removeView(list.get(position));
        container.removeView((View)object);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        if (arg0 == 0) {
            if (pagePosition < 1) {
                viewPager.setCurrentItem(list.size() - 2, false);
            } else if (pagePosition > list.size() - 2) {
                viewPager.setCurrentItem(1, false);
            }
        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int positon) {
        pagePosition = positon;
        if (positon < 1) {
            positon = list.size() - 3;
        } else if (positon > list.size() - 2) {
            positon = 0;
        } else {
            positon--;
        }
        for (int i = 0; i < smallList.getChildCount(); i++) {
            if (i == positon) {
                (smallList.getChildAt(i)).setSelected(true);
//                if(smallParams == null){
//                    smallParams = new LinearLayout.LayoutParams(ViewSizeUtils.dp2px(context, 8), ViewSizeUtils.dp2px(context, 6));
//                }
//                (smallList.getChildAt(i)).setLayoutParams(smallParams);

            } else {
                (smallList.getChildAt(i)).setSelected(false);
//                if(bigParams == null){
//                    bigParams = new LinearLayout.LayoutParams(ViewSizeUtils.dp2px(context, 7), ViewSizeUtils.dp2px(context, 5));
//                }
//                (smallList.getChildAt(i)).setLayoutParams(bigParams);

            }
        }
        if(titleView != null){
            titleView.setText(pics.get(positon).getArticleTitle());
        }
    }

    @Override
    public void onClick(View view) {
        NewsEntity pic = (NewsEntity) view.getTag();
        try{
            TypeConstant.openTypeDetail(pic, (Activity) context,"");
            MobclickAgentUtils.event(Config.EventBauduId.game_076, "推荐列表轮播图点击");
            StatService.onEvent(AppApplication.getInstance(), "067", "推荐列表轮播图点击", 1);
        }catch (Exception e){

        }
//        Intent intent = new Intent(context, NewsDetailActivity.class);
//        intent.putExtra(BaseActivity.PARAMETER1, pic.getArticleId());
//        intent.putExtra(BaseActivity.PARAMETER2, TypeConstant.SOURCETYPE_NORMAL);
//        context.startActivity(intent);
    }

}
